

//JavaScript Graph-it! (Absolute)- by javascriptkit.com
//Visit JavaScript Kit (http://javascriptkit.com) for script
//Credit must stay intact for use
//based heavily on http://www.javascriptkit.com/script/script2/graphit.shtml
//but altered to actually work

const swakgraphimage="chrome://swaksmack/skin/images/poll.png";
const swakgraphuserimage="chrome://swaksmack/skin/images/userpoll.png";
const swakgraphsessionimage="chrome://swaksmack/skin/images/sessionpoll.png";

//YOU CAN DEFINE MULTIPLE GRAPHS, eg:
//var graphy=new Array()


function swakOTSGraphPercent(outaccum,gwidth){
	try {
		var total=0;
		var len = 6;
		for (i=0;i<6;i++) {
			var key = 'tally'+i;
			total+=parseInt(outaccum[key]);
		}
	
		var output='<table border="0" cellspacing="0" cellpadding="0">';
		for (i=0;i<len;i++){
			var key = 'tally'+i;
			var otsans = i+1;
			if (total == 0) {
				calpercentage = 0;
			}
			else {
				calpercentage=Math.round(outaccum[key]*100/total);
			}
			calwidth=Math.round(gwidth*(calpercentage/100));
			output+='<tr><td>'+otsans+'&nbsp;</td><td><img src="'+swakgraphimage+'" width="'+calwidth+'" height="10"> '+calpercentage+'%</td></tr>';
		}
		output+='</table>';
		output+='<br>Total OTS Answers: <b>'+total+'</b>';
		return output;
	}
	catch(x) {
		return "Oops, the extension goofed...hey it's free, what do you expect?";
	}
	//document.write(output+'<br>Total participants: <b>'+total+'</b>')
}

function swakOTSGraphCount(outaccum,gwidth,gheight){
	try {
		if (gheight == null) gheight = 10;
		var len = 6;
		var total=0;
		var output='<table border="0" cellspacing="0" cellpadding="0">';
		for (i=0;i<len;i++){
			var key = 'tally'+i;
			var otsans = i+1;
			total+=outaccum[key];
			var calwidthP=gwidth*(parseInt(outaccum[key])/100);
			output+='<tr><td>'+otsans+'&nbsp;</td><td><img src="'+swakgraphimage+'" width="'+calwidthP+'" height="' + gheight+'"> '+outaccum[key]+'</td></tr>'
		}
		output+='</table>';
		output+='<br>Total OTS: <b>'+total+'</b>';
		return output;
	}
	catch(x) {
		return "Oops, the extension goofed...hey it's free, what do you expect?";
	}
}

function swakOTSGraphCombined(outaccum,gwidth,gheight,dispayUserPicks){
	try {
		if (dispayUserPicks == null) dispayUserPicks = true;
		if (gheight == null) gheight = 10;
		var total=0;
		var len = 6;
		var usertotal =0;
		var calpercentage =0;
		var usercalpercentage=0;
		var scaledusercalpercentage=0;
		var sessionTotal = swakstats.numOTSWins1 + swakstats.numOTSWins2 + swakstats.numOTSWins3 + swakstats.numOTSWins4 + swakstats.numOTSWins5 + swakstats.numOTSWins6;
		var sessionpercentage = 0;
		for (i=0;i<6;i++) {
			var key = 'tally'+i;
			var userkey = 'usertally' +i;
			//total+=parseInt(outaccum[key]);
			total+=outaccum[key];
			usertotal+=outaccum[userkey];
		}
	
		var output='<table border="0" cellspacing="0" cellpadding="0">';
		for (i=0;i<len;i++){
			var key = 'tally'+i;
			var userkey='usertally'+i;
			
			var otsans = i+1;
			var sessionkey = 'numOTSWins'+otsans;
			
			
			if (total == 0) {
				calpercentage = 0;
			}
			else {
				calpercentage=Math.round(outaccum[key]*100/total);
			}
			
			if (usertotal == 0) {		
		 		usercalpercentage = 0;
		 		scaledusercalpercentage =0;
			}
			else {
				usercalpercentage=Math.round(outaccum[userkey]*100/usertotal);
				//use the overal total in order to keep the values of the user picks on the overall scale
				if (total == 0) {
					scaledusercalpercentage=Math.round(outaccum[userkey]*100/usertotal);
				}
				else {
					scaledusercalpercentage=Math.round(outaccum[userkey]*100/total);
				}
			}
			
			if (sessionTotal == 0) {
				sessionpercentage = 0;
			}
			else {
				sessionpercentage = Math.round(swakstats[sessionkey]*100/sessionTotal);
			}
			
			calwidth=Math.round(gwidth*(calpercentage/100));
			usercalcwidth=Math.round(gwidth*(scaledusercalpercentage/100));
			var sessioncalcwidth = Math.round(gwidth*(sessionpercentage/100));
			var calwidthP=gwidth*(parseInt(outaccum[key])/100);
			output+='<tr><td class="swakotscell0">ots '+otsans+'&nbsp;</td><td><img src="'+swakgraphimage+'" width="'+calwidth+'" height="' + gheight + '"> '+outaccum[key]+'  (' +calpercentage + '%)</td></tr>';
			
			output+='<tr><td class="swakotscell2">ots(ses) '+otsans+'&nbsp;</td><td><img src="'+swakgraphsessionimage+'" width="'+sessioncalcwidth+'" height="' + gheight + '"> '+swakstats[sessionkey]+'  (' +sessionpercentage + '%)</td></tr>';
			
			if (dispayUserPicks == true) {
				output+='<tr><td class="swakotsusercell swakotscell0User">user '+otsans+'&nbsp;</td><td class="swakotsusercell swakotscell1User"><img src="'+swakgraphuserimage+'" width="'+usercalcwidth+'" height="' + gheight + '"> '+outaccum[userkey]+'  (' +usercalpercentage + '%)</td></tr>';
				output+='<tr><td class="swakotsusercell swakotscellspace" colSpan=2 ><SPACER height="10" type="block"></td></tr>';		
			}
			else {
				output+='<tr><td style="display:none" class="swakotsusercell swakotscell0User">user '+otsans+'&nbsp;</td><td style="display:none" class="swakotsusercell swakotscell1User"><img src="'+swakgraphuserimage+'" width="'+usercalcwidth+'" height="' + gheight + '"> '+outaccum[userkey]+'  (' +usercalpercentage + '%)</td></tr>';
				output+='<tr><td style="display:none" class="swakotsusercell swakotscellspace" colSpan=2 ><SPACER height="10" type="block"></td></tr>';		
			}
		}
		output+='</table>';
		output+='<br><span class="swakotsgraphtotal">Total OTS Results:  <b>'+total+'</b></span>';
		output+='<br><span class="swakotsgraphusertotal">Total User Results: <b>'+usertotal+'</b></span>';
		output+='<br><span class="swakotsgraphsessiontotal">Total Session Results: <b>'+sessionTotal+'</b></span>';
		return output;
	}
	catch(x) {
		return "Oops, the extension goofed...hey it's free, what do you expect?" + x;
	}
}
